package com.yzhl.batch_sync.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yzhl.batch_sync.mapper.AchieveMapper;
import com.yzhl.batch_sync.mapper.PushMapper;

/**
 * @author wangzu
 * @date 2018/10/12
 */
public class BaseService {

    static final int PAGE_SIZE = 5000;

    public void syncData(AchieveMapper achieveMapper, PushMapper pushMapper) {
        int totalCnt = 0;
        try {
            pushMapper.delete(null);
            int pageNum = 1;
            while (true) {
                Page<Object> page = PageHelper.startPage(pageNum, PAGE_SIZE);
                if (pageNum == 1) {
                    page.setCount(true);
                } else {
                    page.setCount(false);
                }
                achieveMapper.list(null);
                if (pageNum == 1) {
                    totalCnt = Math.toIntExact(page.getTotal());
                }
                pushMapper.insert(page.getResult());
                if (page.size() < PAGE_SIZE) {
                    break;
                }
                pageNum++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("同步数据count:"+ totalCnt);
    }
}
